#include <iostream>
#include <queue>

struct Node {
    int val;
    Node *left;
    Node *right;
};

void level_walk(Node *root)
{
    std::queue<Node *> q;
    q.push(root);

    while(!q.empty()) {
        Node *r = q.front();
        q.pop();

        if (r->left != NULL) {
            q.push(r->left);
        }

        if (r->right != NULL) {
            q.push(r->right);
        }

        std::cout << r->val << " ";
    }

    std::cout << std::endl;
}

void link(Node nodes[], int parent, int left, int right)
{
    if (left != -1) {
        nodes[parent].left = &nodes[left];
    }

    if (right != -1) {
        nodes[parent].right = &nodes[right];
    }
}

int main(int argc, const char *argv[])
{
    Node nodes[7] = {
        {8, NULL, NULL},
        {6, NULL, NULL},
        {10, NULL, NULL},
        {5, NULL, NULL},
        {7, NULL, NULL},
        {9, NULL, NULL},
        {11, NULL, NULL}
    };
    link(nodes, 0, 1, 2);
    link(nodes, 1, 3, 4);
    link(nodes, 2, 5, 6);

    level_walk(nodes);
    return 0;
}
